package com.ultrasoft.farmasoft.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ultrasoft.farmasoft.domain.Operation;
import com.ultrasoft.farmasoft.domain.Product;
import com.ultrasoft.farmasoft.domain.Transaction;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.service.BuyProductsTransactionService;
import com.ultrasoft.farmasoft.service.ReportsService;
import com.ultrasoft.farmasoft.util.UtilCommon;
import com.ultrasoft.farmasoft.util.UtilConverter;

@Controller
@SessionAttributes({"s_UserPharmacy"}) 
@RequestMapping(value="/Reports")
public class ReportsPersonalArchingController {
	private HttpSession session;
	private Userpharmacy s_UserPharmacy;
	private int s_userId;
	private int s_pharmacyId;
	
	private final ReportsService reportsService;
	
	
	@Autowired
	public ReportsPersonalArchingController(ReportsService reportsService){
		this.reportsService = reportsService;
	}
	
	private boolean sessionControl(HttpServletRequest request){
		boolean res = false;
		session = request.getSession(true);
		if(session.getAttribute("s_UserPharmacy") != null){
			s_UserPharmacy 	= (Userpharmacy)session.getAttribute("s_UserPharmacy");
			s_userId 		= s_UserPharmacy.getUser().getUserId();
			s_pharmacyId 	= s_UserPharmacy.getPharmacy().getPharmacyId();
			res = true;
		}
		return res;
	}
	
	private String redirectReportsPersonalArching(HttpServletRequest request){
		if(!sessionControl(request))
			return "redirect:/index"; 
		else
			return "redirect:/personalArching?"+UtilCommon.menuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.menuId_)+"&"+UtilCommon.subMenuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.subMenuId_); 
	}
	
	@RequestMapping(value="/personalArching", method=RequestMethod.GET)
	public String handleRequestReportsPersonalArching(HttpServletRequest request, Model model) throws ServletException {
		if(this.sessionControl(request)){		
			return UtilCommon.fReports_PersonalArching+"personalArching";
		}else
			return redirectReportsPersonalArching(request);
	}
	
	@RequestMapping(value = "/personalArchingSearchByDate", method = RequestMethod.GET)
   	public @ResponseBody List<Transaction> handleRequestPersonalArchingSearchByDate(HttpServletRequest request){	
		return getListTransaction(reportsService.getListTransactionByDateTransaction(UtilConverter.converterStringToDate(request,"dateSearch")));
    }
	private List<Transaction> getListTransaction(List<Transaction> transactionListTemp){
		List<Transaction> transactionList = new ArrayList<Transaction>();
		for (Transaction trTemp : transactionListTemp) {
			Operation op = new Operation(trTemp.getOperationId().getOperationId(),trTemp.getOperationId().getName());
			Transaction tr = new Transaction(trTemp.getTransactionPK().getPeriodIds(),trTemp.getTransactionPK().getTransactionId());
			tr.setAmount(trTemp.getAmount());
			tr.setUnits(trTemp.getUnits());
			tr.setOperationId(op);
			transactionList.add(tr);
		}
		return transactionList;
	}
}
